package com.ghsc.ghxc.jsb.ui.home.activity

import android.os.Message
import android.text.TextUtils
import android.view.View
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.cn.appcore.event.SchedulerTransformer
import com.cn.appcore.ext.asColor
import com.cn.appcore.ext.dp
import com.cn.appcore.ext.getCompatColor
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.showToast
import com.cn.appcore.ext.startKtxActivity
import com.cn.appcore.ext.startKtxActivityKill
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.ActivitySubmitOrderBinding
import com.ghsc.ghxc.jsb.ext.intentExtras
import com.ghsc.ghxc.jsb.ext.setGone
import com.ghsc.ghxc.jsb.ext.setOnFastClickListener
import com.ghsc.ghxc.jsb.ext.setVisible
import com.ghsc.ghxc.jsb.ext.textString
import com.ghsc.ghxc.jsb.ui.home.bean.PrepareBuyBean
import com.ghsc.ghxc.jsb.ui.my.activity.AddressActivity
import com.ghsc.ghxc.jsb.ui.my.bean.AddressDetailBean
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.home.activity
 * 类名：SubmitOrderActivity
 * 创建时间：2024/5/6 15:58
 * 创建人 @author： xiaolin
 * 描述 ：Todo... 提交订单
 */
class SubmitOrderActivity   : BaseVMActivity<SubmitOrderViewModel, ActivitySubmitOrderBinding>(){
    override fun attachLayoutRes(): Int {
        return  R.layout.activity_submit_order
    }

    override fun attachVMClass(): Class<SubmitOrderViewModel>? {
        return SubmitOrderViewModel::class.java
    }


    private val pid: String? by intentExtras("pid","0")
    private val skuId: String? by intentExtras("skuId","0")
    private val count: String? by intentExtras("count","0")



    private var shouhuoid:String = ""
    private var addressDetailBean: AddressDetailBean? = null

    private var prepareBuyBean: PrepareBuyBean? = null
    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }

    override fun initView() {
        mViewModel.onAddressListMethod("")
        binding.apply {
            bar.topTitle.text = "提交订单 "
            bar.topLeftView.setOnClickListener { finish() }
            bar.topRightView.visibility = View.GONE
        }

        rxBusSubscription = RxBus.getDefault().toObservable(RxBusBean::class.java)
            .compose(SchedulerTransformer())
            .subscribe{ rxBusBean ->
               if (rxBusBean.id == CustKey.ADDRESS_INFO && rxBusBean.sign == CustKey.ADDRESS_INFO) {
                    val bean = rxBusBean.anyBean as AddressDetailBean
                    addressDetailBean = bean
                    binding.apply {
                        val shapeBuilder = sclAddress.shapeBuilder
                        tvEmptyAddress.setGone(true)
                        group.setVisible(true)
                        shapeBuilder?.setShapeSolidColor(R.color.white.asColor())
                        shapeBuilder?.setShapeStrokeColor(R.color.white.asColor())
                        shapeBuilder?.setShapeStrokeWidth(1.dp)
                        //默认地址
                        tvName.text = "${bean.name} ${bean.mobile.substring(0,3)}****${bean.mobile.substring(7,bean.mobile.length)}"
                        tvAddress.text = "${bean.province}${if (bean.city == bean.province) "" else bean.city}${bean.area}${bean.site}"
                        shouhuoid = "${bean.id}"
                        val map: HashMap<String, Any> = HashMap()
                        map["pid"] = pid.toString()
                        map["skuid"] = skuId.toString()
                        map["shouhuoid"] = shouhuoid
                        map["cnt"] = count.toString()
                        loge("canshu ="+GsonUtil.mapToJsonStr(map).toString())
                        GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.prepareBuy(it1) }
                    }
                }
            }
    }

    override fun initClickListener() {
        super.initClickListener()
        binding.run {
            sclCoupon.setOnFastClickListener {
//                startActivity(CouponActivity().javaClass)
            }
            sclAddress.setOnFastClickListener {
                startKtxActivity<AddressActivity>(
                    values = arrayListOf(
                        "is_select" to true,
                        "id" to shouhuoid
                    )
                )
            }
            stvSubmitOrder.setOnFastClickListener {
                if (addressDetailBean == null){
                    showToast("请添收货地址")
                    return@setOnFastClickListener
                }
                showToast("暂时无法下单")

            }
        }
    }


    override fun initRequestObserver() {
        super.initRequestObserver()
        binding.apply {
            mViewModel.apply {
                errorMsg.observe(this@SubmitOrderActivity) {
                    dismissLoading()
                    showToast(it)
                }
                againLoginMsg.observe(this@SubmitOrderActivity) {
                    dismissLoading()
                    showToast(CustKey.IN_AGAIN_MSG)
                    LoginHelper.startLoginPage()
                }
                onAddressListData.observe(this@SubmitOrderActivity){
                    it.let {
                        val shapeBuilder = sclAddress.shapeBuilder
                        if (it.isNotEmpty()){
                            //有录入的地址
                            tvEmptyAddress.setGone(true)
                            group.setVisible(true)
                            shapeBuilder?.setShapeSolidColor(R.color.white.asColor())
                            shapeBuilder?.setShapeStrokeColor(R.color.white.asColor())
                            shapeBuilder?.setShapeStrokeWidth(1.dp)
                            it.forEach {
                                if (it.ifdefault == 1){
                                    //默认地址
                                    tvName.text = "${it.name} ${it.mobile.substring(0,3)}****${it.mobile.substring(7,it.mobile.length)}"
                                    tvAddress.text = "${it.province}${if (it.city == it.province) "" else it.city}${it.area}${it.site}"
                                    shouhuoid = "${it.id}"
                                    addressDetailBean = it
                                }
                            }
                        }else {
                            //无地址
                            group.setGone(true)
                            tvEmptyAddress.setVisible(true)
                            shapeBuilder?.setShapeSolidColor(R.color.color_F9F7EF.asColor())
                            shapeBuilder?.setShapeStrokeColor(R.color.color_F8BD00.asColor())
                            shapeBuilder?.setShapeStrokeWidth(1.dp)
                        }
                        shapeBuilder?.into(sclAddress)
                        val map: HashMap<String, Any> = HashMap()
                        map["pid"] = pid.toString()
                        map["skuid"] = skuId.toString()
                        map["shouhuoid"] = shouhuoid
                        map["cnt"] = count.toString()
                        loge("canshu ="+GsonUtil.mapToJsonStr(map).toString())
                        GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.prepareBuy(it1) }
                    }
                }
                prepareBuyResult.observe(this@SubmitOrderActivity){
                    //预下单
                    it.data.let {
                        prepareBuyBean = it.data
                        GlideUtil.ImageLoad(this@SubmitOrderActivity,it.data.ginfo.images,ivCommodityImg)
                        tvCommodityName.text = it.data.ginfo.title
                        binding.tvPayPrice.text="${it.data.ginfo.skuprice}"
                        tvCount.text = "x${it.data.ginfo.cnt}"
                        tvHint.text =  "应付: "
                        binding.tvPayRmb.text="${it.data.ginfo.skuprice}"
                        tvTotalPrice2.text =  "¥ ${ it.data.ginfo.totalprice}"
                        tvYunFei.text= "¥ ${it.data.ginfo.yunfei}"
                    }
                }

            }
        }

    }

}